﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReadyApps.Common;
using ReadyApps.Data.Wiki;
using ReadyApps.Wiki;
using ReadyApps.Wiki.Views;
using ReadyAppsTest.Common;
using ReadyAppsTest.Data;

namespace ReadyAppsTest.Wiki.Views
{
    [TestClass]
    public class EditViewTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Private Method Tests

        [TestMethod]
        [Description("Tests for the proper formatting of the page links JavaScript variable")]
        public void GetAvailablePageLinksJSVariableTest()
        {
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            PageDetails pageDetails = new PageDetails(true, false, false, false, "User");
            TestWikiSecurityManager securityManager = new TestWikiSecurityManager();

            int pageID = 2;
            string baseFolder = "baseFolder";
            EditView target = new EditView(pageID, baseFolder, pageDetails, controlSettings, dataProvider, urlManager, securityManager);
            PrivateObject targetWrapper = new PrivateObject(target);

            dataProvider.GetAvailablePageLinks_ReturnValue = new List<AvailablePageLink>();
            dataProvider.GetAvailablePageLinks_ReturnValue.Add(new AvailablePageLink(1, "page1"));
            string expected = "var pageLinkKeys =\n[\n\t['page1', '1']\n];\n";
            string actual = (string)targetWrapper.Invoke("GetAvailablePageLinksJSVariable");
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(dataProvider.GetAvailablePageLinks_pageID, pageID);
            Assert.AreEqual(dataProvider.GetAvailablePageLinks_folder, baseFolder);

            dataProvider.GetAvailablePageLinks_ReturnValue = new List<AvailablePageLink>();
            dataProvider.GetAvailablePageLinks_ReturnValue.Add(new AvailablePageLink(1, "page1"));
            dataProvider.GetAvailablePageLinks_ReturnValue.Add(new AvailablePageLink(3, "page3"));
            expected = "var pageLinkKeys =\n[\n\t['page1', '1'],\n\t['page3', '3']\n];\n";
            actual = (string)targetWrapper.Invoke("GetAvailablePageLinksJSVariable");
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        [Description("Tests for proper creation even when there are no page links available")]
        public void GetAvailablePageLinksJSVariableEmptyTest()
        {
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            PageDetails pageDetails = new PageDetails(true, false, false, false, "User");
            TestWikiSecurityManager securityManager = new TestWikiSecurityManager();

            int pageID = 2;
            string baseFolder = "baseFolder";
            EditView target = new EditView(pageID, baseFolder, pageDetails, controlSettings, dataProvider, urlManager, securityManager);
            PrivateObject targetWrapper = new PrivateObject(target);

            dataProvider.GetAvailablePageLinks_ReturnValue = new List<AvailablePageLink>();
            string expected = "var pageLinkKeys =\n[\n];\n";
            string actual = (string)targetWrapper.Invoke("GetAvailablePageLinksJSVariable");
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(dataProvider.GetAvailablePageLinks_pageID, pageID);
            Assert.AreEqual(dataProvider.GetAvailablePageLinks_folder, baseFolder);
        }

        [TestMethod]
        [Description("Tests for the proper priority of getting the setting value for the editor control")]
        public void EditorBasePathTest()
        {
            TestAppSettings appSettings = new TestAppSettings();
            ReadyAppsSettings.SetAppSettings(appSettings);
            WikiControlSettings controlSettings;

            List<Tuple<string, string, string>> data = new List<Tuple<string, string, string>>();
            data.Add(new Tuple<string, string, string>(null, null, string.Empty));
            data.Add(new Tuple<string, string, string>("/controlSettings", null, "/controlSettings"));
            data.Add(new Tuple<string, string, string>(null, "/appSettings", "/appSettings"));
            data.Add(new Tuple<string, string, string>("/controlSettings", "/appSettings", "/controlSettings"));

            foreach (var testCase in data)
            {
                controlSettings = new WikiControlSettings() { CKEditorBasePath = testCase.Item1 };
                appSettings.CKEditorBasePath = testCase.Item2;
                PrivateType target = new PrivateType(typeof(EditView));
                string actual = (string)target.InvokeStatic("GetEditorBasePath", controlSettings);
                Assert.AreEqual(testCase.Item3, actual, string.Format("Failed on controlSettings = {0} and appSettings = {1}",
                    testCase.Item1, testCase.Item2));
            }
        }

        #endregion
    }
}
